package com.carpedil.shirodemo.common.config;

import com.carpedil.shirodemo.common.exception.BizRuntimeException;
import com.carpedil.shirodemo.common.util.ResultCode;
import com.carpedil.shirodemo.common.util.ResultVO;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/** @author Administrator */
@RestControllerAdvice
public class GlobalExceptionHandler {

  @ExceptionHandler(value = BizRuntimeException.class)
  public ResultVO bizExceptionHandler(BizRuntimeException e) {
    System.out.println("e = " + e);
    return ResultVO.fail(e.getStatus(), e.getMessage());
  }

  @ExceptionHandler(value = NullPointerException.class)
  public ResultVO exceptionHandler(NullPointerException e) {
    System.out.println("e = " + e);
    return ResultVO.fail(ResultCode.SERVER_INNER_ERROR);
  }

  @ExceptionHandler(value = UnknownAccountException.class)
  public ResultVO unknownAccountExceptionHandler(UnknownAccountException e) {
    System.out.println("e = " + e);
    return ResultVO.fail(ResultCode.USER_NOT_EXIST);
  }

  @ExceptionHandler(value = IncorrectCredentialsException.class)
  public ResultVO incorrectCredentialsExceptionHandler(IncorrectCredentialsException e) {
    System.out.println("e = " + e);
    return ResultVO.fail(ResultCode.USER_PASS_ERROR);
  }

  @ExceptionHandler(value = Exception.class)
  public ResultVO exceptionHandler(Exception e) {
    System.out.println("e = " + e);
    return ResultVO.fail(ResultCode.SERVER_INNER_ERROR);
  }
}
